package com.zxm.oss.service.impl;

import com.zxm.common.exception.file.FileException;
import com.zxm.common.utils.file.FileUtils;
import com.zxm.oss.service.UploadStrategy;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;

/**
 * 抽象上传模板
 *
 * @author silencezheng
 * @date 2021/07/28
 */
@Service
public abstract class AbstractUploadStrategyImpl implements UploadStrategy {
	@Value("${upload.mode}")
	private String uploadMode;

	@Override
	public String uploadFile(MultipartFile file, String path) {
		try {
			// 获取文件md5值
			String md5 = FileUtils.getMd5(file.getInputStream());
			// 获取文件扩展名
			String extName = FileUtils.getExtName(file.getOriginalFilename());
			// 重新生成文件名
			String fileName = md5 + extName;
			//如果是七牛云
			if ("qiniu".equals(uploadMode)||"minio".equals(uploadMode)) {
				return saveFile(file);
			}
			// 判断文件是否已存在
			if (!exists(path + fileName)) {
				// 不存在则继续上传
				upload(path, fileName, file.getInputStream());
			}
			// 返回文件访问路径
			return getFileAccessUrl(path + fileName);
		} catch (Exception e) {
			e.printStackTrace();
			throw new FileException("500",new Object[]{"文件上传失败"});
		}
	}

	@Override
	public String uploadFile(String fileName, InputStream inputStream, String path) {
		try {
			// 上传文件
			upload(path, fileName, inputStream);
			// 返回文件访问路径
			return getFileAccessUrl(path + fileName);
		} catch (Exception e) {
			e.printStackTrace();
			throw new FileException("500",new Object[]{"文件上传失败"});

		}
	}

	/**
	 * 判断文件是否存在
	 *
	 * @param filePath 文件路径
	 * @return {@link Boolean}
	 */
	public abstract Boolean exists(String filePath);

	/**
	 * 上传
	 *
	 * @param path        路径
	 * @param fileName    文件名
	 * @param inputStream 输入流
	 * @throws IOException io异常
	 */
	public abstract void upload(String path, String fileName, InputStream inputStream) throws IOException;

	/**
	 * 获取文件访问url
	 *
	 * @param filePath 文件路径
	 * @return {@link String}
	 */
	public abstract String getFileAccessUrl(String filePath);

	public abstract String saveFile(MultipartFile uploadFile);

}
